define(function(require, exports, module) {
    'use strict';
    var rk = require('rk');
    require('../../su/xsySuButtonBar/widget');


    $.widget('rk.xsyOpportunityButtonBar', $.rk.xsySuButtonBar, {
        _create: function() {
            var me = this;
            var opt = me.options;
            var elem = me.element;

            me._super();
        },

        _selfInit : function(){
            var me = this;
            var opt = me.options;
            var elem = me.element;
            var id = me._getDetailId();

            var buttonData = me._getOwnerData().toolBarData;
            elem.find('[act^=add]').attr('businessId', id);
            elem.find("[act='addContact']").attr('accountId', buttonData.accountId);

        },

        editOppProduct : function(data){
            var me = this;
            var opt = me.options;
            var elem = me.element;
            var id = data.id;
            $(document.body).append('<div id="guider"></div>');

            rk.dialog({
                title: rk.i18n('CRM_CORE_EDIT') + SESSION.keywords.opportunity + '——' + rk.i18n('CRM_CORE_EDIT') + rk.i18n('CRM_CORE_PRODUCT'),
                width: 1020,
                height: 600,
                success: function(contentElem, footerElem) {
                    var tpl = require('oldcrm/tmpl/scrpt_tmpl_createOpportunity_products.tpl');
                    var data = SESSION;
                    var html = rk.templateText(tpl, data);
                    contentElem.append(html);

                    contentElem.opAddProductCtrl({
                        currencyUnit: window.SESSION.currencyUnit,
                        edit: true,
                        bizConfig: {
                            opportunityId: id
                        }
                    });

                    footerElem.find('[act="save"]').click(function() {
                        var products_value = contentElem.opAddProductCtrl('getData');
                        var submit_products = [];
                        for (var pi = 0; pi < products_value.products.length; pi++) {
                            var pItem = products_value.products[pi];

                            //var submit_product_item = {};
                            //submit_product_item.productId = pItem.productId;
                            //submit_product_item.priceUnit = pItem.unitPrice;
                            //submit_product_item.unit = pItem.unit;
                            //submit_product_item.amount = pItem.quantity;
                            //submit_product_item.discount = pItem.discount;
                            //submit_product_item.priceTotal = pItem.priceTotal;
                            //submit_product_item.comment = pItem.comment === undefined ? "" : pItem.comment;
                            //submit_product_item.description = pItem.description;
                            //submit_product_item.packNo = pItem.packNo;
                            //
                            //submit_products.push(submit_product_item);
                            pItem.priceUnit = pItem.unitPrice;
                            pItem.amount = pItem.quantity;
                            pItem.comment = pItem.comment === undefined ? "" : pItem.comment;

                            //submit_products.push(submit_product_item);
                            submit_products.push(pItem);
                        }

                        var url = '/json/crm_opportunity/update-products.action';
                        var postData = {
                            opportunityId: id,
                            products: JSON.stringify(submit_products)
                        };
                        $.postJson(url, postData).done(function(json) {
                            if (json.status == 0) {
                                $.msg(rk.i18n('CRM_CORE_EDIT') + rk.i18n('CRM_CORE_SUCCESS'), 1);
                                rk.closeDialog();
                                me._triggerOuterEvent('reloadRefer', {
                                    entity: 'product'
                                });
                            } else {
                                $.msg(rk.i18n('ERROR_SYSTEM') + json.statusText);
                            }
                        });
                    });

                    footerElem.find('[act="cancel"]').click(function() {
                        rk.closeDialog();
                    });

                },
                footer: ['cancel', 'save']
            });

        },

        transferOpportunity : function(data){
            var me = this;
            var opt = me.options;
            var elem = me.element;

            data.entityName = window.SESSION.keywords.opportunity;
            data.url = '/json/crm_opportunity/transfer.action';
            data.postJson = {
                opportunityIds : data.id
            }
            data.callback = function(json){
                var data = $.parseJSON(json.data);
                if(data.permission == true){
                    window.location.reload();
                } else{
                    window.location.href = "/opportunity.action"
                }  
            };

            me.transferEntity(data);
        },

        viewOpportunityLog : function(data){
            var me = this;
            var opt = me.options;
            var elem = me.element;
            data.postJson = {
                opportunityId : data.id
            }
            data.url = '/json/crm_opportunity/entity-logs.action';
            me.viewEntityLog(data)
        },

        deleteOpportunity : function(data){
            var me = this;
            var opt = me.options;
            var elem = me.element;

            data.tip = rk.i18n('ACCOUNT_DELETE_ARRAY_STRING_4', $.htmlEncode(data.name));
            data.postJson = {
                opportunityIds : data.id
            };
            data.url = "/json/crm_opportunity/delete.action";
            data.callback = function(){window.location.href = '/opportunity.action'};

            me.deleteEntity(data);
        },
        //分众提审
        focusMediaSubmitOpp : function(data){
            var me = this;
            var opt = me.options;
            var elem = me.element;

            var oppId = data.id;
            var oppName = data.name;
            $.confirm("是否对当前" + SESSION.keywords.opportunity + "进行报备？", function () {
                $.postJson('/json/crm_opportunity/change-check-status.action', {
                    opportunityId: oppId,
                    checkStatus: 2
                }).done(function (json) {
                    if (json.status == 0) {
                        $.msg('报备成功', 1);
                        window.location.reload();
                    } else if (json.status == 1234568) {
                        $.msg('尚未关联邮件，请执行关联邮件后再进行报备工作');
                    } else if (json.status == 1234567) {
                        $.msg('没有相关联系人或联系人未上传名片');
                    } else if (json.status == 1234569) {
                        $.msg('客户信息不完整，请补全后再报备');
                    } else {
                        $.msg('报备失败');
                    }
                }).fail(function (def, code, message) {
                    def.status != 0 && $.msg('请求失败，请稍后再试');
                });
            });
        },
        //分众审核
        focusMediaCheckOpp : function(data){
            var me = this;
            var opt = me.options;
            var elem = me.element;

            //todo 以后再搞
        },
        //杰赛科技 投标文件编制
        chinaGciAddTask1 : function(data){
            var me = this;
            var opt = me.options;
            var elem = me.element;
            data.chinaGciTaskName = "投标文件编制";
            me._chinaGciAddTask(data);
        },
        //杰赛科技 投标文件审核
        chinaGciAddTask2 : function(data){
            var me = this;
            var opt = me.options;
            var elem = me.element;
            data.chinaGciTaskName = "投标文件审核";
            me._chinaGciAddTask(data);
        },
        //杰赛科技 现场唱标通报
        chinaGciAddTask3 : function(data){
            var me = this;
            var opt = me.options;
            var elem = me.element;
            data.chinaGciTaskName = "现场唱标通报";
            me._chinaGciAddTask(data);
        },
        //杰赛科技 中标结果通报
        chinaGciAddTask4 : function(data){
            var me = this;
            var opt = me.options;
            var elem = me.element;
            data.chinaGciTaskName = "中标结果通报";
            me._chinaGciAddTask(data);
        },
        //杰赛科技 项目经理任命
        chinaGciAddTask5 : function(data){
            var me = this;
            var opt = me.options;
            var elem = me.element;
            data.chinaGciTaskName = "项目经理任命";
            me._chinaGciAddTask(data);
        },
        _chinaGciAddTask : function(data){
            var me = this;
            var opt = me.options;
            var elem = me.element;

            var businessId = data.id;
            var businessName = data.name;
            var chinagci = data.chinagci;

            elem.addTaskForNewDetail({
                business: 3,
                businessId: businessId,
                businessName: businessName,
                chinagci: chinagci,
                chinaGciTaskName: data.chinaGciTaskName,
                whose: 'yours'
            });
        }

    });
});
